package smecta.client.print.controller.product;

import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.converter.DefaultStringConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import smecta.client.print.domain.ScpProductProperty;
import smecta.client.print.domain.ScpProductPropertyValuelist;
import smecta.client.print.mapper.ScpProductPropertyMapper;
import smecta.client.print.mapper.ScpProductPropertyValuelistMapper;
import smecta.client.print.util.CustomTextFieldTableCell;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class PropertyController {

    @Autowired
    private ScpProductPropertyValuelistMapper scpProductPropertyValuelistMapper;

    @Autowired
    private ScpProductPropertyMapper scpProductPropertyMapper;

    @FXML
    private TableView<Item> propertyDictTable;

    @FXML
    private TableColumn<Item, String> codeColumn;

    @FXML
    private TableColumn<Item, String> nameColumn;

    public void manualInit(String code) {
        // 初始化数据
        List<Item> items = new ArrayList<>();
        List<ScpProductPropertyValuelist> list = scpProductPropertyValuelistMapper.selectScpProductPropertyValuelistList(new ScpProductPropertyValuelist(null, code, null));
        String label = list.size() > 0 ? scpProductPropertyMapper.selectScpProductPropertyList(new ScpProductProperty(null, code, null)).get(0).getName() : null;
        list.forEach(item -> {
            Item it = new Item(label, item.getValue());
            items.add(it);
        });
        // 可以添加更多数据...
        propertyDictTable.getItems().clear();
        propertyDictTable.getItems().addAll(items);
        propertyDictTable.refresh();
    }

    @FXML
    public void initialize() {
        propertyDictTable.setEditable(true); // 使表格可编辑
        codeColumn.setCellValueFactory(new PropertyValueFactory<>("code"));

        // 设置单元格值工厂和单元格工厂
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        nameColumn.setCellFactory((col -> new CustomTextFieldTableCell<>(new DefaultStringConverter())));

        // 监听编辑提交事件
        nameColumn.setOnEditCommit(event -> {
            event.getRowValue().setName(event.getNewValue());
        });
    }

    public void add(String code) {
        String label = scpProductPropertyMapper.selectScpProductPropertyList(new ScpProductProperty(null, code, null)).get(0).getName();
        propertyDictTable.getItems().add(new Item(label, "双击编辑-回车生效"));
        propertyDictTable.refresh(); // 刷新表格显示新数据
    }

    public void del() {
        // 这里实现增加新项的逻辑，例如弹出对话框获取用户输入
        // 假设我们添加了一个带有默认值的新项
        propertyDictTable.getItems().remove(propertyDictTable.getItems().size() - 1);
        propertyDictTable.refresh(); // 刷新表格显示新数据
    }

    public void save(String code) {
        List<ScpProductPropertyValuelist> all = scpProductPropertyValuelistMapper.selectScpProductPropertyValuelistList(new ScpProductPropertyValuelist(null, code, null));
        for (int i = 0; i < all.size(); i++) {
            scpProductPropertyValuelistMapper.deleteScpProductPropertyValuelistById(all.get(i).getId());
        }

        propertyDictTable.getItems().forEach(item -> {
            List<ScpProductPropertyValuelist> list = scpProductPropertyValuelistMapper.selectScpProductPropertyValuelistList(new ScpProductPropertyValuelist(null, code, item.getName()));
            if (CollectionUtils.isEmpty(list)) {
                scpProductPropertyValuelistMapper.insertScpProductPropertyValuelist(new ScpProductPropertyValuelist(null, code, item.name));
            }
        });
    }

    // 内部类，用于表示表格中的项
    public static class Item {
        private String code;
        private String name;

        public Item(String code, String name) {
            this.code = code;
            this.name = name;
        }

        // Getter和Setter
        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}